<template>
  <div>
    <el-row class="fun-header">
      <el-col :span="8">
        <el-button type="primary" @click="lockUser">锁定</el-button>
        <el-button type="primary" @click="unLockUser">解锁</el-button>
      </el-col>
      <el-col :span="16">
        <span style="color: rgba(0,0,0,0)">'</span>
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      ref="mainTable"
      border
      @row-click="rowClick"
      @selection-change="handleSelectionChange"
      class="user-main-table"
      style="width:100%">
      <el-table-column type="selection"></el-table-column>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="sysId" label="工号"></el-table-column>
      <el-table-column prop="introduction" label="名称"></el-table-column>
      <el-table-column prop="name" label="登录名"></el-table-column>
      <el-table-column prop="telPhone" label="手机号"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{scope.row.status | userStatus}}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="180px" >
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="userPermissionEdit">
            编辑 <i class="el-icon-edit"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="margin-top: 8px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>

    <!-- 弹框编辑用户权限弹框 -->
    <el-dialog
      width="80%"
      title="用户权限管理"
      :visible.sync="userPermissionDialog"
      append-to-body>
      <hr>
    </el-dialog>

  </div>
</template>

<script>
  import byUpload from '@/components/byUpload'
  import * as userAPI from '@/api/user'
  export default {
    name: 'user',
    components: {
      byUpload
    },
    mounted() {
      this.getList()
    },
    data() {
      return {
        isCollapse: true,
        userPermissionDialog: false,
        list: [],
        tableData: [],
        selectRow: [],
        page: {
          current: 1,
          size: 10,
          total: 0,
          condition: {
            name: '',
            lspId: '',
            mobile: ''
          }
        }
      }
    },
    methods: {
      getList() {
        const that = this
        userAPI.getList(that.page).then(res => {
          this.tableData = res.data
          that.page.current = res.current
          that.page.size = res.size
          that.page.total = res.total
        }).catch(err => {
          console.log(err)
        })
      },
      // 行选中
      rowClick(row, event, column) {
        // 标记当前选中行
        this.$refs.mainTable.clearSelection()
        this.$refs.mainTable.toggleRowSelection(row)
      },
      // 表格改变时
      handleSelectionChange(val) {
        this.selectRow = val
      },
      // 锁定用户
      lockUser() {
        const l = this.selectRow.length
        if (l === 0) {
          this.$message({
            message: '未选择',
            type: 'warning',
            duration: 2 * 1000
          })
        } else {
          const arr = this.selectRow.find(a => a.status === '0')
          if (arr) {
            this.$message({
              message: '有用户已锁定，请重新选择',
              type: 'warning',
              duration: 2 * 1000
            })
          } else {
            // 发送锁定请求
            const arr = []
            for (let i = 0; i < l; i++) {
              arr.push(this.selectRow[i].id)
            }
            userAPI.lock({
              ids: arr
            }).then(res => {
              if (res.success) {
                this.$message({
                  message: res.message,
                  type: 'success',
                  duration: 2 * 1000
                })
                this.getList()
              }
            }).catch(err => {
              console.log(err)
            })
          }
        }
      },
      // 解锁用户
      unLockUser() {
        const l = this.selectRow.length
        if (l === 0) {
          this.$message({
            message: '未选择',
            type: 'warning',
            duration: 2 * 1000
          })
        } else {
          const arr = this.selectRow.find(a => a.status === '1')
          if (arr) {
            this.$message({
              message: '有用户已解锁，请重新选择',
              type: 'warning',
              duration: 2 * 1000
            })
          } else {
            // 发送解锁请求
            const arr = []
            for (let i = 0; i < l; i++) {
              arr.push(this.selectRow[i].id)
            }
            userAPI.unLock({
              ids: arr
            }).then(res => {
              if (res.success) {
                this.$message({
                  message: res.message,
                  type: 'success',
                  duration: 2 * 1000
                })
                this.getList()
              }
            }).catch(err => {
              console.log(err)
            })
          }
        }
      },
      // 编辑用户权限
      userPermissionEdit() {
        this.userPermissionDialog = true
      },
      handleSizeChange(val) {
        this.page.size = val
        this.page.current = 1
        this.getList()
      },
      handleCurrentChange(val) {
        this.page.current = val
        this.getList()
      }
    }
  }
</script>

<style type="text/scss" lang="scss" scoped>

</style>
